package com.anton46.collectionitempicker;

import ohos.agp.components.AttrSet;
import ohos.agp.components.element.Element;

/**
 * 自定义属性工具类
 * <p>
 * 注意： 宽高都为 match_content 且无实际内容时构造方法不会调用
 * 使用方法：
 * xxx extends Component
 * 获取自定义属性：
 * String count = AttrUtils.getStringFromAttr(attrSet,"cus_count","0");
 * <p>
 * 属性定义：
 * 布局头中加入  xmlns:hap="http://schemas.huawei.com/apk/res/ohos" 使用hap区分自定义属性与系统属性
 * 即可使用 hap:cus_count="2"  不加直接使用ohos:cus_count="2"
 *
 *
 * <p>
 *
 */
public class AttrUtils {

    public final static String CP_ITEM_RADIUS = "cp_itemRadius";
    public final static String CP_ITEM_BACKGROUND_NORMAL = "cp_itemBackgroundNormal";
    public final static String CP_ITEM_BACKGROUND_PRESSED = "cp_itemBackgroundPressed";
    public final static String CP_ADD_ICON = "cp_addIcon";
    public final static String CP_CANCEL_ICON = "cp_cancelIcon";
    public final static String CP_ITEM_MARGIN = "cp_itemMargin";
    public final static String CP_ITEM_TEXT_COLOR = "cp_itemTextColor";
    public final static String CP_TEXT_PADDING_LEFT = "cp_textPaddingLeft";
    public final static String CP_TEXT_PADDING_RIGHT = "cp_textPaddingRight";
    public final static String CP_TEXT_PADDING_TOP = "cp_textPaddingTop";
    public final static String CP_TEXT_PADDING_BOTTOM = "cp_textPaddingBottom";
    public final static String CP_SIMPLIFIED = "cp_simplified";


        private AttrUtils() {

        }

        /**
         *
         * @param attrSet
         * @param name
         * @param defaultValue
         * @return String
         */
        public static String getStringFromAttr(AttrSet attrSet, String name, String defaultValue) {
            String value = defaultValue;
            try {
                if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                    value = attrSet.getAttr(name).get().getStringValue();
                }
            } catch (Exception e) {
                return value;
            }
            return value;
        }

        /**
         *
         * @param attrSet
         * @param name
         * @param defaultValue
         * @return Integer
         */
        public static Integer getDimensionFromAttr(AttrSet attrSet, String name, Integer defaultValue) {
            Integer value = defaultValue;
            try {
                if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                    value = attrSet.getAttr(name).get().getDimensionValue();
                }
            } catch (Exception e) {
                return value;
            }
            return value;
        }

        /**
         *
         * @param attrSet
         * @param name
         * @param defaultValue
         * @return Integer
         */
        public static Integer getIntegerFromAttr(AttrSet attrSet, String name, Integer defaultValue) {
            Integer value = defaultValue;
            try {
                if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                    value = attrSet.getAttr(name).get().getIntegerValue();
                }
            } catch (Exception e) {
                return value;
            }
            return value;
        }

    /**
     *
     * @param attrSet
     * @param name
     * @param defaultValue
     * @return Integer
     */
    public static Integer getMediaFromAttr(AttrSet attrSet, String name, Integer defaultValue) {
        Integer value = defaultValue;
        try {
            if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                value = Integer.valueOf(attrSet.getAttr(name).get().getStringValue().replace("$media:",""));
            }
        } catch (Exception e) {
            return value;
        }
        return value;
    }

        /**
         *
         * @param attrSet
         * @param name
         * @param defaultValue
         * @return float
         */
        public static float getFloatFromAttr(AttrSet attrSet, String name, float defaultValue) {
            float value = defaultValue;
            try {
                if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                    value = attrSet.getAttr(name).get().getFloatValue();
                }
            } catch (Exception e) {
            }
            return value;
        }

        /**
         *
         * @param attrSet
         * @param name
         * @param defaultValue
         * @return Long
         */
        public static Long getLongFromAttr(AttrSet attrSet, String name, Long defaultValue) {
            Long value = defaultValue;
            try {
                if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                    value = attrSet.getAttr(name).get().getLongValue();
                }
            } catch (Exception e) {
            }
            return value;
        }

        /**
         *
         * @param attrSet
         * @param name
         * @param defaultValue
         * @return int 颜色
         */
        public static int getColorFromAttr(AttrSet attrSet, String name, int defaultValue) {
            int value = defaultValue;
            try {
                if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                    value = attrSet.getAttr(name).get().getColorValue().getValue();

                }
            } catch (Exception e) {
                return value;
            }
            return value;
        }

        /**
         *
         * @param attrSet
         * @param name
         * @param defaultValue
         * @return boolean
         */
        public static boolean getBooleanFromAttr(AttrSet attrSet, String name, boolean defaultValue) {
            boolean value = defaultValue;
            try {
                if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                    value = attrSet.getAttr(name).get().getBoolValue();
                }
            } catch (Exception e) {
                return value;
            }
            return value;
        }

        /**
         *
         * @param attrSet
         * @param name
         * @param defaultElement
         * @return Element
         */
        public static Element getElementFromAttr(AttrSet attrSet, String name, Element defaultElement) {
            Element element = defaultElement;
            try {
                if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                    element = attrSet.getAttr(name).get().getElement();
                }
            } catch (Exception e) {
                return element;
            }
            return element;
        }




    /**
     *
     * @param attrSet
     * @param name
     * @param defaultValue
     * @return Integer
     */
    public static Integer getgetResourceIdFromAttr(AttrSet attrSet, String name, Integer defaultValue) {
        Integer value = defaultValue;
        try {
            if (attrSet.getAttr(name) != null && attrSet.getAttr(name).isPresent()) {
                value = Integer.valueOf(attrSet.getAttr(name).get().getStringValue().replace("$layout:",""));
            }
        } catch (Exception e) {
            return value;
        }
        return value;
    }

    }
